﻿using System;
using System.Web;
using System.Web.Mvc;
using KMF.WebUI.Infrastructure;
using KMF.DAL;
using KMF.Utility;
using KMF.WebUI.Areas.KMFadmin.Models;

namespace KMF.WebUI.Areas.KMFadmin.Controllers
{
    public class AdminLoginController : Controller
    {
        //
        // GET: /Admin/AdminLogin/

        public ActionResult Login()
        {
            if (Session[SessionConfig.SessionAdminLogged] != null)
            {
                return RedirectToAction("Index", "AdminHome");
            }
            return View();
        }

        [HttpPost]
        public ActionResult Login(AdminLoginModel model,  bool rememberMe )
        {
            if (ModelState.IsValid)
            {
                var user = UserDb.GetByUserName(model.UserName);
                if (user != null)
                {
                    var password = model.Password.CreatePasswordHash(user.PasswordSalt);
                    if (password.Equals(user.Password) && model.UserName.Equals(user.UserName))
                    {
                        // create session
                        Session[SessionConfig.SessionAdminLogged] = new AdminLoginModel
                        {
                            Id = user.Id,
                            UserName = user.UserName,
                            FullName = user.FullName,
                            Email = user.Email
                        };
                        // create cookies
                        if(rememberMe)
                        {
                            var loginCookie = new HttpCookie("loginCookie");
                            var loginCookieGuid = Guid.NewGuid().ToString();
                            loginCookie["Id"] = user.Id.ToString();
                            loginCookie["UserName"] = user.UserName;
                            loginCookie["FullName"] = user.FullName;
                            loginCookie["Email"] = user.Email; 
                            loginCookie["loginCookieGuid"] = loginCookieGuid;
                            loginCookie["loginCookiePass"] = user.Password;
                            loginCookie["cookiesHash"] = user.Password.CreatePasswordHash(loginCookieGuid);
                            loginCookie.Expires = DateTime.Now.AddDays(30);
                            Response.Cookies.Add(loginCookie);
                        }

                        return RedirectToAction("Index", "AdminHome");
                    }
                    ModelState.AddModelError("ErrorSummary", "UserName hoặc mật khẩu không đúng!");
                }
                ModelState.AddModelError("ErrorSummary", "UserName hoặc mật khẩu không đúng!");
            }
            return View(model);
        }

        public JsonResult Logout()
        {
            var data = false;
            if (Session[SessionConfig.SessionAdminLogged] !=null)
            {
                // xoa session
                Session[SessionConfig.SessionAdminLogged] = null;

                // xoa cookie
                var loginCookie = Request.Cookies["loginCookie"];
                if (loginCookie != null)
                {
                    loginCookie.Expires = DateTime.Now.AddDays(-1d);
                    Response.Cookies.Add(loginCookie);
                }
                data = true;
            }
            return Json(data, JsonRequestBehavior.AllowGet);
        }
    }
}
